package mail;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class QQ_Email_N {
    public static void  main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void send_email() throws IOException, AddressException, MessagingException{


        String to = "Juzheng.Lu1@allegion.com";
//        String subject = "subject";
        String c ="静女其姝，俟我于城隅。爱而不见，搔首踟蹰。静女其娈，贻我彤管。彤管有炜，说怿女美。自牧归荑，洵美且异。匪女之为美，美人之贻。";
        
        String username ="87484612@qq.com";
        System.out.println("username:="+username);
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator(username, "fqrmoakwltpibjif11");
        System.out.println("2");
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);  
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        
        mailMessage.setFrom(new InternetAddress("87484612@qq.com"));
        
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象

        System.out.println("3");
        for(int i=1;i<=50;i++){
        	try {
				Thread.sleep(15000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            int a1=(int)((Math.random()+1)*15);
            int b1=(int)((Math.random()+2)*15);
            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            String content = c.substring(62-b1, 62-a1);
            html.setContent(content.trim(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);
            String subject = c.substring(a1, b1);
        	mailMessage.setSubject(subject, "UTF-8");
            Transport.send(mailMessage);
            System.out.println("send i ="+i);
        }
        System.out.println("4");
    }
}
